﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DomainModel;
using NHibernate;
using DAL;

namespace DALTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            TestFixtureSetup();
            Setup();
        }

        private ISession _session;
        private SessionManager _helper;
        private NHibernateSample _sample;

        public void Setup()
        {
            _session = _helper.GetSession();
            _sample = new NHibernateSample(_session);
        }

        private void button1_Click(object sender, EventArgs e)
        {

            Unit customer = _sample.GetUnitById(GetID());
            int customerId = customer.UnitId;
            Display(customer);
        }

        private void Display(Unit customer)
        {
            label1.Text = customer.Name;
        }

        private int GetID()
        {
            return int.Parse(textBox1.Text);
        }

        public void TestFixtureSetup()
        {
            _helper = new SessionManager();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            IList<Unit> units = _sample.From();
            Unit[] kk=units.ToArray<Unit>();
            listBox1.Items.AddRange(kk);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            IList<string> units = _sample.Select();



            listBox2.Items.AddRange(units.ToArray<string>());
        }

        private void button4_Click(object sender, EventArgs e)
        {
            IList<object[]> units = _sample.SelectObject();



            listBox3.Items.AddRange(units[0]);
        }

        private void listBox4_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button5_Click(object sender, EventArgs e)
        {
            IList<Unit> units = _sample.Where();



            listBox3.Items.AddRange(units.ToArray<Unit>());
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void button6_Click(object sender, EventArgs e)
        {
            IList<Unit> units = _sample.GetCustomersByFirstname(textBox2.Text);



            label2.Text = units[0].UnitId.ToString();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            IList<Unit> units = _sample.CreateCriteria();



            listBox5.Items.AddRange(units.ToArray<Unit>());
        }

        private void button8_Click(object sender, EventArgs e)
        {
            Unit unit = new Unit();

            unit.Name = textBox3.Text;


            unit.UnitId = _sample.CreateUnit(unit);

            label3.Text = unit.UnitId.ToString();
        }

        private void button9_Click(object sender, EventArgs e)
        {
            Unit unit = new Unit();


            unit.Name = textBox5.Text;


            unit.UnitId =int.Parse(textBox4.Text);

            _sample.DeleteUnit(unit);

            
        }
     

      

    }
}
